namespace Xsxiaosa.Net
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;

    internal sealed class FtpParse
    {
        // Methods
        private FtpParse()
        {
        }

        public static string EndPointToString(IPEndPoint endPoint)
        {
            byte[] numArray1 = ProxySocket.GetAddressBytes(endPoint.Address);
            int num1 = endPoint.Port;
            object[] objArray1 = new object[6];
            objArray1[0] = numArray1[0];
            objArray1[1] = numArray1[1];
            objArray1[2] = numArray1[2];
            objArray1[3] = numArray1[3];
            objArray1[4] = ((num1 >> 8) & 255);
            objArray1[5] = (num1 & 255);
            return string.Format("{0},{1},{2},{3},{4},{5}", objArray1);
        }

        public static string ParseDescription(string text)
        {
            char[] chArray1 = new char[2];
            chArray1[0] = '\n';
            chArray1[1] = '\r';
            return text.Substring(4, (text.IndexOfAny(chArray1) - 4)).Trim();
        }

        public static long ParseFileLength(string text)
        {
//jyj modify            long num1;
            object[] objArray1;
            try
            {
                return long.Parse(FtpParse.ParseDescription(text));
            }
            catch
            {
                objArray1 = new object[1];
                objArray1[0] = "SIZE";
                throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "CannotParse", objArray1), FtpExceptionStatus.ServerProtocolViolation);
            }
//jyj modify            return num1;
        }

        public static DateTime ParseMdtm(string res)
        {
 //jyj modify           DateTime time1;
            object[] objArray1;
            string text1 = FtpParse.ParseDescription(res);
            if (text1.Length < 14)
            {
                objArray1 = new object[1];
                objArray1[0] = "MDTM";
                throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "CannotParse", objArray1), FtpExceptionStatus.ServerProtocolViolation);
            }
            if (text1.Length > 18)
            {
                objArray1 = new object[1];
                objArray1[0] = "MDTM";
                throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "CannotParse", objArray1), FtpExceptionStatus.ServerProtocolViolation);
            }
            if (text1.Length == 15)
            {
                objArray1 = new object[1];
                objArray1[0] = "MDTM";
                throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "CannotParse", objArray1), FtpExceptionStatus.ServerProtocolViolation);
            }
            if (text1.Length == 14)
            {
                text1 = string.Concat(text1, ".0");
            }
            text1 = text1.PadRight(18, '0');
            try
            {
                return new DateTime(int.Parse(text1.Substring(0, 4)), int.Parse(text1.Substring(4, 2)), int.Parse(text1.Substring(6, 2)), int.Parse(text1.Substring(8, 2)), int.Parse(text1.Substring(10, 2)), int.Parse(text1.Substring(12, 2)), int.Parse(text1.Substring(15, 3)));
            }
            catch
            {
                objArray1 = new object[1];
                objArray1[0] = "MDTM";
                throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "CannotParse", objArray1), FtpExceptionStatus.ServerProtocolViolation);
            }
//jyj modify            return time1;
        }

        public static string ParseReplyDirectory(string description)
        {
            int num2;
            description = FtpParse.ParseDescription(description);
//jyj modify            if (description.Chars[0] != '\"')
			if (description[0] != '\"')
            {
                return null;
            }
            int num1 = 1;
        Label_0017:
            num2 = description.IndexOf('\"', num1);
            if (num2 < num1)
            {
                return null;
            }
            if (num2 == (description.Length - 1))
            {
                return description.Substring(1, (num2 - 1));
            }
//jyj modify            if (description.Chars[(num2 + 1)] != '\"')
			if (description[(num2 + 1)] != '\"')
            {
                return description.Substring(1, (num2 - 1));
            }
            num1 = (num2 + 2);
            goto Label_0017;
        }

        public static IPEndPoint ParseResponseEndPoint(string text, IPEndPoint remoteHost)
        {
            int num1;
            char ch1;
            int num2;
            string[] textArray2;
            uint num3;
            int num4;
            string text1 = FtpParse.ParseDescription(text);
            string text2 = "";
            for (num1 = 0; (num1 < text1.Length); num1 += 1)
            {
//jyj modify                ch1 = text1.Chars[num1];
				ch1 = text1[num1];
//jyj modify                if ((ch1 != ' ') || ((text2.Length != 0) && (text2.Chars[(text2.Length - 1)] != ' ')))
				if ((ch1 != ' ') || ((text2.Length != 0) && (text2[(text2.Length - 1)] != ' ')))
                {
                    if ((ch1 >= '0') && (ch1 <= '9'))
                    {
                        text2 = string.Concat(text2, ch1);
                    }
                    else if ((ch1 == '.') || (ch1 == ','))
                    {
                        text2 = string.Concat(text2, 46);
                    }
                    else if (ch1 == ' ')
                    {
                        text2 = string.Concat(text2, 32);
                    }
                }
            }
            char[] chArray1 = new char[2];
            chArray1[0] = ' ';
            chArray1[1] = '.';
            text2 = text2.Trim(chArray1);
            chArray1 = new char[1];
            chArray1[0] = ' ';
            string[] textArray1 = text2.Split(chArray1);
            for (num2 = 0; (num2 < textArray1.Length); num2 += 1)
            {
                chArray1 = new char[2];
                chArray1[0] = ' ';
                chArray1[1] = '.';
                text2 = textArray1[num2].Trim(chArray1);
                if ((text2.Length >= 11) && (text2.Length <= 23))
                {
                    chArray1 = new char[1];
                    chArray1[0] = '.';
                    textArray2 = text2.Split(chArray1);
                    if (textArray2.Length == 6)
                    {
                        try
                        {
                            num3 = ((uint) (((byte.Parse(textArray2[0]) + (byte.Parse(textArray2[1]) << 8)) + (byte.Parse(textArray2[2]) << 16)) + (byte.Parse(textArray2[3]) << 24)));
                            num4 = ((int) ((byte.Parse(textArray2[4]) << 8) + byte.Parse(textArray2[5])));
//jyj modify                            return new IPEndPoint(((ulong) num3), num4);
							return new IPEndPoint(((long) num3), num4);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            object[] objArray1 = new object[1];
            objArray1[0] = "PASV";
            throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "CannotParse", objArray1), FtpExceptionStatus.ServerProtocolViolation);
        }

        public static string ParseStoreUniqueFile(string description)
        {
            char[] chArray1;
            if (description.StartsWith("FILE:"))
            {
                return description.Substring(5).Trim();
            }
            int num1 = description.LastIndexOf(" for ");
            if (num1 > 0)
            {
                num1 += 5;
            }
            else
            {
                num1 = description.LastIndexOf(" ");
                if (num1 > 0)
                {
                    num1 += 1;
                }
            }
            if (num1 > 0)
            {
                chArray1 = new char[2];
                chArray1[0] = '.';
                chArray1[1] = ' ';
                return description.Substring(num1).Trim(chArray1);
            }
            object[] objArray1 = new object[1];
            objArray1[0] = "STOU";
            throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "CannotParse", objArray1), FtpExceptionStatus.ServerProtocolViolation);
        }

        public static string[] SplitRawList(Encoding encoding, MemoryStream buffer)
        {
            string text1 = encoding.GetString(buffer.GetBuffer(), 0, ((int) buffer.Length));
            char[] chArray1 = new char[2];
            chArray1[0] = '\n';
            chArray1[1] = '\r';
            text1 = text1.TrimEnd(chArray1).Replace("\r\n", "\n");
            if (text1.Length == 0)
            {
                return new string[0];
            }
            chArray1 = new char[1];
            chArray1[0] = '\n';
            return text1.Split(chArray1);
        }

    }}

